package com.renren.webmaven2.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.renren.component.utils.FileUtils;
import com.renren.webmaven2.util.DateUtil;

@Service
public class FileUploadService {

	private Log logger = LogFactory.getLog(getClass());

	// private final String UPLOAD_PATH = "D:\\data\\eliya\\";
	// private final String TEMP_PATH = "D:\\temp1\\buffer\\";
	private final String VIEW_PATH = "/home/yangjizhong24yycamn0g0jyioz7h9ovnsgm2k4/wwwroot/";
	private final String UPLOAD_PATH = VIEW_PATH + "images/";
	private final String TEMP_PATH = VIEW_PATH + "temp1/buffer/";

	@SuppressWarnings("unchecked")
	public List<String> uploadFileByFileItem(HttpServletRequest request) {
		String uploadPath = UPLOAD_PATH;
		String tempPath = TEMP_PATH;
		FileUtils.createDirectory(tempPath);
		List<String> fileNames = new ArrayList<String>();
		try {
			// Create a factory for disk-based file items
			DiskFileItemFactory factory = new DiskFileItemFactory();

			// Set factory constraints
			factory.setSizeThreshold(4096 * 1000); //
			factory.setRepository(new File(tempPath));//

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

			// Set overall request size constraint
			upload.setSizeMax(4 * 1024 * 1024); //

			List<FileItem> items = upload.parseRequest(request);//
			Iterator<FileItem> i = items.iterator();
			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				String fileName = fi.getName();
				if (fileName != null) {
					File savedFile = new File(uploadPath, fileName);
					fi.write(savedFile);
					fileNames.add(fileName);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileNames;
	}

	public String uploadFile(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
		if (StringUtils.isEmpty(multipartFile.getOriginalFilename())) {
			return "";
		}
		String day = DateUtil.formatDate(new Date(), DateUtil.DATE_PATTEN_DAY);
		String newPath = UPLOAD_PATH + day + File.separator;
		FileUtils.createDirectory(newPath);
		logger.info("createDirectory:" + newPath);
		String newName = RandomUtils.nextInt(100) + "" + RandomUtils.nextInt(100) + "" + RandomUtils.nextInt(100);
		if ("image/jpeg".equals(multipartFile.getContentType())) {
			newName = newName + ".jpg";
		} else if ("image/gif".equals(multipartFile.getContentType())) {
			newName = newName + ".gif";
		} else {
			return "";
		}
		FileUtils.writeToFile(newPath + newName, multipartFile.getBytes());
		return day + File.separator + newName;
	}
}
